/**
 * 
 */
package com.kexinde.wjifen.dao;

import com.kexinde.wjifen.model.User;
import com.kexinde.wjifen.util.CreateRandomNum;
import com.kexinde.wjifen.util.JdbcTemplate;
import com.kexinde.wjifen.vo.RegistUser;

/**
 * 用户数据库操作类
 * 
 * @author vincent
 * 
 */
public class UserDao {

	/**
	 * 注册时添加用户
	 * @param regUser
	 * @return
	 */
	public boolean add(RegistUser regUser) throws Exception{
		//生成不重复的微号
		String weiNum = CreateRandomNum.create(9);
		int times = 10;
		while(this.isWeiNumExist(weiNum)&&times>0){
			weiNum = CreateRandomNum.create(9);
			times--;
		}
		User user = new User(0L,regUser.getName(), regUser.getPassword(),
				regUser.getTelNum(), weiNum, regUser.getEmail(), 0, "",
				regUser.getInitTotalPoint());
		Object[] params = new Object[8];
		String sql = "INSERT INTO tb_user(name,password,telNum,email,purview,imagePath,totalPoint,weiNum)"
				+ "values(?,?,?,?,?,?,?,?)";
		params[0] = user.getName();
		params[1] = user.getPassword();
		params[2] = user.getTelNum();
		params[3] = user.getEmail();
		params[4] = user.getPurview();
		params[5] = user.getImagePath();
		params[6] = user.getTotalPoint();
		params[7] = user.getWeiNum();

		return JdbcTemplate.executeUpdate(sql, params);

	}
	
	/**
	 * 登录成功时，查询用户所有信息
	 * @param name 要查询的用户名
	 * @return 用户对象
	 */
	public User findByName(String name) throws Exception{
		String sql = "SELECT id,name,password,telNum,weiNum,Email,Purview,imagePath,totalPoint" +
		" FROM tb_user where name = ?";
		Object [] params = {name};
		return JdbcTemplate.query("com.kexinde.wjifen.model.User", sql, params, 9);
	}
	
	
	/**
	 * 检查用户名是否已经存在
	 * @param name 要检查的名字
	 * @return 布尔值
	 */
	public boolean isNameExist(String name) throws Exception{
		String sql = "SELECT COUNT(*) FROM tb_user WHERE name=?";
		Object [] params = {name};
		int result = JdbcTemplate.count(sql,params);
		return (result>=1)?true:false;
	}
	
	/**
	 * 检查微号是否已经存在
	 * @param weiNum 要检查的微号
	 * @return 布尔值
	 */
	public boolean isWeiNumExist(String weiNum) throws Exception{
		String sql = "SELECT COUNT(*) FROM tb_user WHERE weiNum=?";
		Object [] params = {weiNum};
		int result = JdbcTemplate.count(sql,params);
		return (result>=1)?true:false;
	}
	
	/**
	 * 检查对应的用户密码是否正确
	 * @param name 要检查的用户名
	 * @param password 要检查的用户密码
	 * @return 布尔值
	 */
	public boolean isPasswordRight(String name,String password)throws Exception{
		String sql = "SELECT COUNT(*) FROM tb_user WHERE name=? AND password=?";
		Object [] params = {name,password};
		int result = JdbcTemplate.count(sql,params);
		return (result>=1)?true:false;
	}
	
	/**
	 * 修改用户密码
	 * @param id
	 * @param password
	 * @return
	 * @throws Exception
	 */
	public boolean modifyPassword(Long id,String password)throws Exception{
		String sql = "Update tb_user SET password=? WHERE id=?";
		Object [] params = {password,id};
		return JdbcTemplate.executeUpdate(sql, params);
	}
	
	

}
